package com.ling.pl.security.view.user;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.ling.core.common.utils.D7PageUtils;
import com.ling.pl.core.commons.utils.AssertHelper;
import com.ling.pl.security.feign.DefaultOrgService;
import com.ling.pl.security.feign.DefaultRoleService;
import com.ling.pl.security.feign.DefaultUserService;
import com.ling.pl.security.model.DefaultOrgVO;
import com.ling.pl.security.model.DefaultRoleVO;
import com.ling.pl.security.model.DefaultUserVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bo.wang
 * @since 2013-1-29
 */
@Component("ling2.userMaintain")
public class UserMaintain {
    @Resource
    DefaultRoleService roleService;
    @Resource
    private DefaultUserService userService;
    @Resource
    private DefaultOrgService orgService;

    @DataProvider
    public void loadUsers(Page<DefaultUserVO> page, Criteria criteria) throws Exception {
        D7PageUtils.daoPageToPage(userService.findDefaultUserByPage(D7PageUtils.buildQueryParams(criteria, page)), page);
    }

    @DataProvider
    public void loadUsers(Page<DefaultUserVO> page, Map parameter, Criteria criteria) throws Exception {
        if (AssertHelper.notEmpty(parameter)) {
            String roles = (String) parameter.get("roles");
            System.out.println(roles);
            if (AssertHelper.notEmpty(roles)) {
                parameter.put("roles", roles.split(","));
            }
        } else {
            parameter = new HashMap();
        }
        D7PageUtils.buildQueryParams(parameter, page);
        D7PageUtils.daoPageToPage(userService.findDefaultUserByPage(parameter), page);
    }

    @DataProvider
    public Collection<DefaultOrgVO> loadUserOrgs(String username) {
        return userService.loadUserOrgs(username);
    }

    @DataProvider
    public Collection<DefaultRoleVO> loadUserRoles(String username) {
        return userService.loadUserRoles(username);
    }

    @DataResolver
    public void saveUsers(Collection<DefaultUserVO> users) throws Exception {
//        Map allrecodsMap = D7PageUtils.assembleDatasetMap(users);
//        userService.saveMap(allrecodsMap);
        for (Object record : users) {
            EntityState state = EntityUtils.getState(record);

            if (state.equals(EntityState.NEW)) {
                DefaultUserVO vo = (DefaultUserVO) record;
                userService.create(vo);
            }

        }
    }

    /**
     * 这个方法用来判断在添加新用户时用户名是否已经存在
     *
     * @param username 用户输入的用户名
     */
    @Expose
    public String userIsExists(String username) {
        String result = userService.userIsExists(username);
        return result;
    }

    /**
     * 重置指定用户密码
     *
     * @param username
     * @return
     */
    @Expose
    public String resetPassword(String username) {
        return userService.resetPassword(username);
    }

    @Expose
    public void insertUserPosition(String username, String ids) {
        userService.insertUserPosition(username, ids);
    }

    @Expose
    public void insertUserOrg(String username, String ids) {
        userService.insertUserOrg(username, ids);
    }

}
